<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @package DomAr
* @subpackage Parser
*/
class DomArClassParserMethod {
	
	/**
	* @param String Method name
	*/
	protected $name;
	
	/**
	* @param String Class name which method it is
	*/
	protected $className;
	
	/**
	* @param Array 
	*/
	protected $arguments;
	
	/**
	* @param String Original source code
	*/
	protected $code;
	
	public function __construct( $className, $name ) {
		$this->name = $name;
		$this->className = $className;
	}
	
	/**
	* Parses method
	* @param DomArClassParser $class
	* @param Array $tokens All class file tokens
	* @param Int $startTokenPosition Position where method definition starts.
	*/
	final public static function parse( DomArClassParser $class, &$tokens, $startTokenPosition ) {
		// Parsime doc commenti.
		$orm = array();
		if( $tokens[$startTokenPosition-2][0] == T_DOC_COMMENT )  {
			$orm = DomArAnnotationParser::parse( $tokens[$startTokenPosition-2][1], 'method', $class->file, $tokens[$startTokenPosition-2][2]);
		}
		// -------------------
		
		$name = '';
		$arguments = array();
		$code = '';
		
		$braceLevel = 0;
		$nrOfTokens = count($tokens);
		if( $tokens[$startTokenPosition-2][0] == T_ABSTRACT ) {
			$code .= 'abstract ';
		}

		for( $s=$startTokenPosition; $s<$nrOfTokens; $s++) {
			
			// peame arvestust sügavuse üle.
			if( $tokens[$s] == '{' )
				$braceLevel++;
			else if( $tokens[$s] == '}' ) {
				$braceLevel--;
				if( $braceLevel == 0) {
					$code .= '}';
					break;
				}
			} else if( $braceLevel == 0 && $tokens[$s] == ';' ) {
				$code .= ';';
				break;
			}
			
			
			// meetodi nimi
			if( $tokens[$s][0] == T_FUNCTION && $funcLevel == 0 ) {
				for( $t = $s+1; $t<$nrOfTokens; $t++ ) {
					if( $tokens[$t][0] == T_STRING ) {
						$name = $tokens[$t][1];
						break;
					}
				}
			}
			// ------------------------------
			
			
			// meetodi argumendid.
			if( $tokens[$s] == '(' && $argumentsChecked != true ) {
				for( $t = $s+1; $t<$nrOfTokens; $t++ ) {

					if( $tokens[$t] == ')' ) {
						$argumentsChecked = true;
						break;
					}
					
					if( $tokens[$t][0] == T_VARIABLE ) {
						$arguments[] = substr($tokens[$t][1],1);
					}
					
				}
			}
			// ------------------------------
			
			
			
			// rewriting method.
			if( is_array($tokens[$s]) )
				$code .= $tokens[$s][1];
			else
				$code .= $tokens[$s];
			// ------------------------------
				
		}
		$startTokenPosition = $s;
		
		
		// finalize
		$recordMethod = false;
		foreach( $orm as $command ) {
			switch( $command['handler'] ) {
				case '__access':
					$recordMethod = true;
				break;
				default:
					throw new RuntimeException('Unknown command for method <strong>'.$this->name.'</strong> in class <strong>'.$this->className.'</strong>. Command: '.implode(' ',$command));
				break;
			}
		}
		if( $recordMethod === true ) {
			$method = new DomArClassParserRecordMethod( $class->name, $name );
		} else {
			$method = new DomArClassParserMethod( $class->name, $name );
		}
		$method->code = $code;
		$method->arguments = $arguments;
		$method->setOrm( $orm );
		// ----------------
		
		return $method;
	}
	
	
	public function setOrm( $orm ) {
		
	}
	
	public function getClass() {
		return DomArClassParser::get($this->className);
	}
	
	public function __get( $var ) {
		return $this->$var;
	}
}
?>